package org.example.controller;

import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.example.service.leave.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/leave")
public class LeaveController {

    @Autowired
    LeaveService leaveService;

    /** 部署流程定义 */
    @GetMapping("/deployWithClassPath")
    public String deployWithClassPath() {
        RepositoryService rs = ProcessEngines.getDefaultProcessEngine().getRepositoryService();
        Deployment deployment = rs.createDeployment()
                .name("leave请假流程")
                .addClasspathResource("processes/leave.bpmn")
                .deploy();
        rs.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        return deployment.getId();
    }

    /** 开启一个流程实例 */
    @GetMapping("/startProcess")
    public String startProcess(@RequestParam(value = "orderNo") String orderNo) {
        return leaveService.startProcess(orderNo);
    }

    /** 发起请假 */
    @GetMapping("/askForLeave")
    public String askForLeave(@RequestParam(value = "taskId") String taskId) {
        return leaveService.askForLeave(taskId);
    }

    /** 项目经理审核 */
    @GetMapping("/projectReview")
    public String projectReview(@RequestParam(value = "taskId") String taskId,
                                @RequestParam(value = "projectFlag") String projectFlag,
                                @RequestParam(value = "reason", required = false) String reason) {
        return leaveService.projectReview(taskId, projectFlag, reason);
    }

    /** 部门经理审核 */
    @GetMapping("/departmentReview")
    public String departmentReview(@RequestParam(value = "taskId") String taskId,
                                   @RequestParam(value = "departFlag") String departFlag,
                                   @RequestParam(value = "reason", required = false) String reason) {
        return leaveService.departmentReview(taskId, departFlag, reason);
    }

    /** 修改单据 */
    @GetMapping("/editOrder")
    public String editOrder(@RequestParam(value = "taskId") String taskId,
                            @RequestParam(value = "leaveDays") int leaveDays) {
        return leaveService.editOrder(taskId, leaveDays);
    }

    /** 登记确认 */
    @GetMapping("/confirmOrder")
    public String confirmOrder(@RequestParam(value = "taskId") String taskId) {
        return leaveService.confirmOrder(taskId);
    }
}
